
package me.liuhui.mall.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.liuhui.mall.common.base.vo.ResultVO;
import me.liuhui.mall.common.service.FileService;
import me.liuhui.mall.common.service.dto.SaveAdHtmlDTO;
import me.liuhui.mall.manager.service.AdSpaceService;
import me.liuhui.mall.manager.service.convertor.AdItemConverter;
import me.liuhui.mall.manager.service.convertor.AdSpaceConverter;
import me.liuhui.mall.manager.service.dto.ad.space.CreateAdSpaceDTO;
import me.liuhui.mall.manager.service.dto.ad.space.ListAdSpaceDTO;
import me.liuhui.mall.manager.service.dto.ad.space.ModifyAdSpaceDTO;
import me.liuhui.mall.manager.service.vo.ad.item.AdItemVO;
import me.liuhui.mall.manager.service.vo.ad.space.AdSpaceVO;
import me.liuhui.mall.manager.service.vo.ad.space.ListAdSpaceVO;
import me.liuhui.mall.repository.mapper.AdItemMapper;
import me.liuhui.mall.repository.mapper.AdSpaceMapper;
import me.liuhui.mall.repository.model.AdItem;
import me.liuhui.mall.repository.model.AdSpace;
import me.liuhui.mall.repository.model.enums.AdItemStatus;
import me.liuhui.mall.tools.utils.FreemarkerUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created on 2020/10/14 20:12
 * <p>
 * Description: [TODO]
 * <p>
 *
 * @author [清远]
 */
@Service
public class AdSpaceServiceImpl implements AdSpaceService {
    @Value("${mall.file.domain}")
    private String domain;
    @Resource
    private AdSpaceConverter adSpaceConverter;
    @Resource
    private AdItemConverter adItemConverter;
    @Resource
    private AdSpaceMapper adSpaceMapper;
    @Resource
    private AdItemMapper adItemMapper;
    @Resource
    private FileService fileService;

    @Override
    public ResultVO<ListAdSpaceVO> list(ListAdSpaceDTO dto) {
        LambdaQueryWrapper<AdSpace> wrapper = Wrappers.lambdaQuery();
        IPage<AdSpace> page = new Page<>(dto.getPage(), dto.getLimit());
        IPage<AdSpace> adSpaceIPage = adSpaceMapper.selectPage(page, wrapper);
        ListAdSpaceVO vo = new ListAdSpaceVO();
        vo.setTotal(adSpaceIPage.getTotal());
        vo.setList(adSpaceConverter.toVo(adSpaceIPage.getRecords()));
        return ResultVO.success(vo);
    }

    @Override
    public ResultVO<AdSpaceVO> detail(Long id) {
        AdSpace adSpace = adSpaceMapper.selectById(id);
        if (adSpace == null) {
            return ResultVO.buildFailResult("广告位不存在");
        }
        AdSpaceVO adSpaceVO = adSpaceConverter.toVo(adSpace);
        LambdaQueryWrapper<AdItem> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdItem::getAdSpaceId, id)
                .orderByDesc(AdItem::getWeight);
        List<AdItem> adItems = adItemMapper.selectList(wrapper);
        adSpaceVO.setAdItemVos(adItemConverter.toVo(adItems));
        return ResultVO.success(adSpaceVO);
    }

    @Override
    public ResultVO<Boolean> publish(Long id) {
        ResultVO<AdSpaceVO> detailVo = detail(id);
        if (!detailVo.getSuccess()) {
            return ResultVO.buildFailResult(detailVo.getMsg());
        }
        AdSpaceVO adSpaceVo = detailVo.getData();
        adSpaceVo.setDomain(domain);
        String html = FreemarkerUtil.genHtml(adSpaceVo.getNo(), adSpaceVo.getTemplate(), adSpaceVo);
        SaveAdHtmlDTO dto = new SaveAdHtmlDTO();
        dto.setNo(adSpaceVo.getNo());
        dto.setHtml(html);
        fileService.saveAdHtml(dto);
        for (AdItemVO adItemVO : adSpaceVo.getAdItemVos()) {
            AdItem adItem = new AdItem();
            adItem.setId(adItemVO.getId());
            adItem.setStatus(AdItemStatus.PUBLISHED.getCode());
            adItemMapper.updateById(adItem);
        }
        return ResultVO.success();
    }

    @Override
    public ResultVO<Boolean> create(CreateAdSpaceDTO dto) {
        AdSpace entity = adSpaceConverter.createDtoToEntity(dto);
        entity.setCreateTime(new Date());
        adSpaceMapper.insert(entity);
        return ResultVO.success();
    }

    @Override
    public ResultVO<Boolean> modify(ModifyAdSpaceDTO dto) {
        AdSpace adSpace = adSpaceMapper.selectById(dto.getId());
        if (adSpace == null) {
            return ResultVO.buildFailResult("广告位不存在");
        }
        AdSpace entity = adSpaceConverter.modifyDtoToEntity(dto);
        adSpaceMapper.updateById(entity);
        return ResultVO.success();
    }

    @Override
    public ResultVO<Boolean> delete(Set<Long> ids) {
        for (Long id : ids) {
            AdSpace adSpace = adSpaceMapper.selectById(id);
            if (adSpace == null) {
                return ResultVO.buildFailResult("广告位不存在！");
            }
            SaveAdHtmlDTO dto = new SaveAdHtmlDTO();
            dto.setNo(adSpace.getNo());
            dto.setHtml("");
            fileService.saveAdHtml(dto);
            adItemMapper.deleteBySpaceId(id);
            adSpaceMapper.deleteById(id);

        }
        return ResultVO.success();
    }

}
